package org.dromara.wmscloud.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.wmscloud.domain.bo.BaseMatClassBo;
import org.dromara.wmscloud.domain.vo.BaseMatClassVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.wmscloud.domain.vo.BaseMatGroupVo;
import org.dromara.wmscloud.domain.bo.BaseMatGroupBo;
import org.dromara.wmscloud.service.IBaseMatGroupService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 物料组
 *
 * @author hou
 * @date 2025-04-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wmsCloud/matGroup")
public class BaseMatGroupController extends BaseController {

    private final IBaseMatGroupService baseMatGroupService;

    /**
     * 查询物料组
     */
    @SaCheckPermission("base:group:list")
    @GetMapping("/listAll")
    public R <List<BaseMatGroupVo>> listAllMatGroup() {
        BaseMatGroupBo bo = new BaseMatGroupBo();
        List<BaseMatGroupVo> list = baseMatGroupService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 查询物料组列表
     */
    @SaCheckPermission("base:group:list")
    @GetMapping("/list")
    public TableDataInfo<BaseMatGroupVo> list(BaseMatGroupBo bo, PageQuery pageQuery) {
        return baseMatGroupService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出物料组列表
     */
    @SaCheckPermission("base:group:export")
    @Log(title = "物料组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseMatGroupBo bo, HttpServletResponse response) {
        List<BaseMatGroupVo> list = baseMatGroupService.queryList(bo);
        ExcelUtil.exportExcel(list, "物料组", BaseMatGroupVo.class, response);
    }

    /**
     * 获取物料组详细信息
     *
     * @param groupId 主键
     */
    @SaCheckPermission("base:group:query")
    @GetMapping("/{groupId}")
    public R<BaseMatGroupVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long groupId) {
        return R.ok(baseMatGroupService.queryById(groupId));
    }

    /**
     * 新增物料组
     */
    @SaCheckPermission("base:group:add")
    @Log(title = "物料组", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseMatGroupBo bo) {
        return toAjax(baseMatGroupService.insertByBo(bo));
    }

    /**
     * 修改物料组
     */
    @SaCheckPermission("base:group:edit")
    @Log(title = "物料组", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseMatGroupBo bo) {
        return toAjax(baseMatGroupService.updateByBo(bo));
    }

    /**
     * 删除物料组
     *
     * @param groupIds 主键串
     */
    @SaCheckPermission("base:group:remove")
    @Log(title = "物料组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{groupIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] groupIds) {
        return toAjax(baseMatGroupService.deleteWithValidByIds(List.of(groupIds), true));
    }
}
